package com.github.abel533.postprocessor.instantiation;

import com.github.abel533.C;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.stereotype.Component;

import java.lang.reflect.Constructor;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @author liuzh
 */
@Component
public class InstantiationPostProcessorImpl extends InstantiationAwareBeanPostProcessorAdapter {
    private Set<String> beanNames = new HashSet<>(10);

    public InstantiationPostProcessorImpl() {
        super();
        beanNames.add("user");
        beanNames.add("userExt");
        //beanNames.add("org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat");
    }

    void print(String key, String beanName){
//        if(beanNames.size() < 10){
//            beanNames.add(beanName);
//        }
        if (beanNames.contains(beanName)) {
            C.print(key + " - " + beanName);
        }
    }

    @Override
    public Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {
        //执行频繁并且没什么用
        //print("SmartInstantiationAwareBeanPostProcessor#predictBeanType", beanName);
        return super.predictBeanType(beanClass, beanName);
    }

    @Override
    public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException {
        print("SmartInstantiationAwareBeanPostProcessor#determineCandidateConstructors", beanName);
        return super.determineCandidateConstructors(beanClass, beanName);
    }

    @Override
    public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
        print("SmartInstantiationAwareBeanPostProcessor#getEarlyBeanReference", beanName);
        return super.getEarlyBeanReference(bean, beanName);
    }

    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        print("InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation", beanName);
        return super.postProcessBeforeInstantiation(beanClass, beanName);
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        print("InstantiationAwareBeanPostProcessor#postProcessAfterInstantiation", beanName);
        return super.postProcessAfterInstantiation(bean, beanName);
    }

    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        print("InstantiationAwareBeanPostProcessor#postProcessProperties", beanName);
        return super.postProcessProperties(pvs, bean, beanName);
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        print("BeanPostProcessor#postProcessBeforeInitialization", beanName);
        return super.postProcessBeforeInitialization(bean, beanName);
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        print("BeanPostProcessor#postProcessAfterInitialization", beanName);
        return super.postProcessAfterInitialization(bean, beanName);
    }

}
